import common from "../../utils/public.js"
import http from "../../utils/httputil.js"

var navIdOut = "";
var pageNum = 0;
var navIdx = 0;
var pageSize = 7;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIdx:0,
    scrLeft:0,
    minHit:0,
    wkLists:[],
    onLoaded:false
  },

  //点击导航特效
  clickNav(res){
    wx.showLoading({
      title: '数据加载中...',
      mask:true
    })
    if(res.currentTarget){    
      var navIdx=res.currentTarget.dataset.idx
      var navId=res.currentTarget.dataset.id
    }else{
      var navIdx=res.navIdx
      var navId=res.navId
    }
    navIdOut=navId;    

    this.setData({
      navIdx,
      navId,
      wkLists:[]
    }) 
    this.getWkList(navId)
  
  },

  // 获取活动距离
  getScrLeft(navIdx){
    var itemWth=wx.getSystemInfoSync().windowWidth/5
    var scrLeft=(navIdx-2)*itemWth    
    if(scrLeft<0){
      scrLeft=0
    }
    this.setData({
      scrLeft
    })
  },

  //获取导航列表
  getNavData(){
    let that = this;
    http.post('index/blog/cate.html', {}, function(e){
      if(e.code == 200){
        that.setData({
          navLists:e.data
        })
        that.getScrLeft(navIdx) 
      }
    })
  },

  //获取真实的作品列表
  getWkList(cat_id, page = 1){ 
    let that = this;
    pageNum = page;
    that.setData({
      onLoaded:true
    })
    wx.showLoading({
      title: '数据加载中...',
      mask:true
    })
    http.post('index/blog/index.html', {
      cat_id: cat_id,
      page_size: pageSize,
      page: pageNum
    }, function(res){
      if(res.code == 200){
        var list = res.data.data;
        if(list.length==0 || list.length<pageSize){
          that.setData({
            onLoaded:false
          })
        }
        list.forEach(item=>{
          item.title=common.getStrLen(item.title,25)
        })
        var oldList=that.data.wkLists;
        var newList=oldList.concat(list)

        that.setData({
          wkLists:newList          
        })
        wx.hideLoading()
        wx.stopPullDownRefresh() 
  
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
      }
    })
  },

  //获取最小高度
  getWinHit(){
    var winHit= wx.getSystemInfoSync().windowHeight-100
    this.setData({
      minHit:winHit
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {   
    var id=options.id;
    var idx=options.idx
    navIdx=idx;
    navIdOut=id
    this.clickNav({navIdx,navIdOut});
    this.getNavData();
    this.getWinHit();
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      wkLists:[]
    })
    this.getWkList(navIdOut);
    this.getNavData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  
  onReachBottom: function () {
    pageNum++
    this.getWkList(navIdOut,pageNum)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})